<?php

namespace Dcat\Admin\Grid\Filter;

use Illuminate\Support\Facades\Schema;

class Year extends Date
{
    /**
     * {@inheritdoc}
     */
    protected $query = 'whereYear';

    /**
     * @var string
     */
    protected $fieldName = 'year';

    protected function buildCondition(...$params)
    {
        $model = $this->parent->model()->repository()->model();
        $table = $model->getTable();
        $column = $this->column;

        if (Schema::hasColumn($table, $column)) {
            $type = Schema::getColumnType($table, $column);

            // 如果是字符串类型，改用 where 等于查询
            if (in_array($type, ['string', 'text'])) {
                $this->query = 'where';
            } else {
                $this->query = 'whereYear';
            }
        }

        return parent::buildCondition(...$params);
    }
}
